package com.zs.lib_base.ext

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.Uri

/**
 * descrition:
 */
/**
 * 跳转到miui的权限管理页面
 */
fun gotoMiuiPermission(context:Context) {
    try { // MIUI 8
        val localIntent = Intent("miui.intent.action.APP_PERM_EDITOR")
        localIntent.setClassName(
            "com.miui.securitycenter",
            "com.miui.permcenter.permissions.PermissionsEditorActivity"
        )
        localIntent.putExtra("extra_pkgname", context.packageName)
        context.startActivity(localIntent)
    } catch (e: Exception) {
        try { // MIUI 5/6/7
            val localIntent = Intent("miui.intent.action.APP_PERM_EDITOR")
            localIntent.setClassName(
                "com.miui.securitycenter",
                "com.miui.permcenter.permissions.AppPermissionsEditorActivity"
            )
            localIntent.putExtra("extra_pkgname", context.packageName)
            context.startActivity(localIntent)
        } catch (e1: Exception) { // 否则跳转到应用详情
            context.startActivity(getAppDetailSettingIntent(context))
        }
    }
}

/**
 * 华为的权限管理页面
 */
fun gotoHuaweiPermission(context:Context) {
    try {
        val intent = Intent()
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        val comp = ComponentName(
            "com.huawei.systemmanager",
            "com.huawei.permissionmanager.ui.MainActivity"
        )//华为权限管理
        intent.component = comp
        context.startActivity(intent)
    } catch (e: Exception) {
        e.printStackTrace()
        context.startActivity(getAppDetailSettingIntent(context))
    }

}

/**
 * 跳转到魅族的权限管理系统
 */
fun gotoMeizuPermission(context:Context) {
    try {
        var intent = Intent()

        intent = Intent("com.meizu.safe.security.SHOW_APPSEC")
        intent.addCategory(Intent.CATEGORY_DEFAULT)
        intent.putExtra("packageName", "com.jwell.index")
        context.startActivity(intent)
    } catch (e: Exception) {
        e.printStackTrace()
        context.startActivity(getAppDetailSettingIntent(context))
    }
}

/**
 * 获取应用详情页面intent（如果找不到要跳转的界面，也可以先把用户引导到系统设置页面）
 */
fun getAppDetailSettingIntent(context: Context): Intent {
    val localIntent = Intent()
    localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    localIntent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"
    localIntent.data = Uri.fromParts("package", context.packageName, null)
    return localIntent
}

